﻿﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace TurtlePoker
{
    enum CardType
    {
        Heart, // 红桃
        Spade, // 黑桃
        Club, // 梅花
        Diamond, // 方块
        SmallJoker,
        BigJoker,
    }

    class Card : IComparable
    {
        public int m_code;
        public CardType m_type;

        public Card(int code)
        {
            Debug.Assert(code < 54);
            this.m_code = code;
            if (code == 52)
                m_type = CardType.SmallJoker;
            else if (code == 53)
                m_type = CardType.BigJoker;
            else {
                int type = code / 13;
                switch (type)
                {
                    case 0:
                        m_type = CardType.Heart;
                        break;
                    case 1:
                        m_type = CardType.Spade;
                        break;
                    case 2:
                        m_type = CardType.Club;
                        break;
                    case 3:
                        m_type = CardType.Diamond;
                        break;
                }
            }
        }

        public override string ToString()
        {
            string type = "";
            switch (this.m_type)
            {
                case CardType.Heart:
                    type = "♥";
                    break;
                case CardType.Spade:
                    type = "♠";
                    break;
                case CardType.Club:
                    type = "♣";
                    break;
                case CardType.Diamond:
                    type = "♦";
                    break;
                case CardType.SmallJoker:
                case CardType.BigJoker:
                    type = "×";
                    break;
            }
            string val = "";
            if (this.m_code < 52)
            {
                val = (this.m_code % 13 + 1).ToString();
                if (this.m_code % 13 == 0)
                    val = "A";
                if (this.m_code % 13 == 10)
                    val = "J";
                if (this.m_code % 13 == 11)
                    val = "Q";
                if (this.m_code % 13 == 12)
                    val = "K";
            }
            else
            {
                if (this.m_code == 52)
                    val = "SmallJoker";
                if (this.m_code == 53)
                    val = "BigJoker";
            }

            return string.Format("[{0}-{1}]", type, val);
        }

        public static bool operator ==(Card c1, Card c2)
        {
            return c1.m_code == c2.m_code;
        }

        public static bool operator !=(Card c1, Card c2)
        {
            return c1.m_code != c2.m_code;
        }

        public int CompareTo(object obj)
        {
            Card c = obj as Card;
            return this.m_code.CompareTo(c.m_code);
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            Card c = obj as Card;
            return c == this;
        }

        public override int GetHashCode()
        {
            return this.m_code.GetHashCode();
        }
    }
}
